/**
 * 
 */
package com.pms.framework;

import java.io.IOException;
import java.util.Hashtable;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * @author Ram
 *
 */

public class ScreenFlowManagerServlet extends HttpServlet {
	

	private Hashtable htActionMapping;
	
	public void service(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
		try {
			//page and event id for getting the forward list from action-mapping xml	
			String pageId=null;
			String eventId=null;
			if(request.getAttribute("pageId")!=null){
				pageId = (String)request.getAttribute("pageId");
				eventId = (String)request.getAttribute("eventId");
			}else{
			 pageId = request.getParameter("pageId");
			eventId = request.getParameter("eventId");
			}
			
			//application object
			ServletContext application = getServletContext();
			
			//getting action mapping hashtable from application
			htActionMapping =(Hashtable)application.getAttribute("actionMapping");
			
			//getting forwardlists
			ActionMap actionMap = (ActionMap) htActionMapping.get(pageId + "_" + eventId);
			if (actionMap == null) {
				System.out.println(
						"There was no Action Map for Page:-" + pageId
								+ " and Event:-" + eventId);
				throw new Exception(
						"No action mapped for the Event of the Page");
			}
			
			//forward list in hashtable from action-mapping.xml
			Hashtable forwardHT = actionMap.getForwardList();
			
			
			
			//instance of RequestValueObject from request set in froncontroller
			//RequestValueObject requestValueObject =(RequestValueObject) request.getAttribute("requestValueObject");
			
			//token for selection the next view page from requestValueObject set in action class
			String nextPageToken = (String)request.getAttribute("nextPageToken");
			System.out.println(forwardHT+"--------nextPageToken----------------"+nextPageToken);
			//next page name from the action mapping xml depends on the value of nextPageToken
			String nextPage = (String) forwardHT.get(nextPageToken);
			System.out.println("--------nextPageToken----------------"+nextPageToken);
			String metaContent = (String) forwardHT.get("MetaContent");
			
			//setting jsp name as "/jsp/pagename"
			request.setAttribute("nextPage",nextPage);
			request.setAttribute("metaContent",metaContent);
			System.out.println( "Next Page set to - " + nextPage);
			
			
			
			
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occure
	 */
	public void init() throws ServletException {

	}

}
